package com.base.caselib;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.caselib.bean.CaseListAjBean;
import com.base.caselib.databinding.ActivityCaselistajBinding;
import com.base.caselib.presenter.CaseListAjPresenter;
import com.base.caselib.view.CaseListAjView;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.base.adapter.BaseAdapter;
import com.base.commonlib.base.adapter.BaseHolder;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.recy.utils.SpaceItemDecoration;
import com.base.commonlib.resh.api.RefreshLayout;
import com.base.commonlib.resh.footer.ClassicsFooter;
import com.base.commonlib.resh.header.ClassicsHeader;
import com.base.commonlib.resh.listener.OnRefreshLoadmoreListener;
import com.base.commonlib.utils.GlideUtil;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by weikailiang on 2019/11/28.
 */
@Route(path = "/case/caselistaj")
public class CaseListAjActivity extends CBaseBarMvpActivity<CaseListAjPresenter> implements CaseListAjView{
    private ActivityCaselistajBinding mBinding;
    public List<CaseListAjBean> mData = new ArrayList<>();
    private BaseAdapter<CaseListAjBean> mAdapter;
    private String keyWord = "";
    private String ids = "";
    public int index = 1;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_caselistaj;
    }

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityCaselistajBinding) mRootBinding;
        mPresenter = new CaseListAjPresenter(this,this);
        mPresenter.start();
        setBackNavigation();
        setBarTitle("案件管理");
        setRigImg(R.drawable.ic_search, new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        mBinding.rl.setRefreshHeader(new ClassicsHeader(mContext));
        mBinding.rl.setHeaderHeight(60);
        mBinding.rl.setRefreshFooter(new ClassicsFooter(mContext));
//        Disposable disposable = RxBus2.getInstance().doSubscribe(SearchBean.class, new Consumer<SearchBean>() {
//            @Override
//            public void accept(SearchBean bean) throws Exception {
//                if (bean != null){
//                    mPresenter.mSearchBean = bean;
//                    mBinding.rl.autoRefresh();
//                }
//            }
//        });
//        RxBus2.getInstance().addSubscription(this, disposable);

        mBinding.etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    index = 1;
                    mPresenter.getCaseList(index,keyWord,ids);
                    mBinding.rl.autoRefresh();
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
                        if (getCurrentFocus() != null)
                            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                }
                return false;
            }
        });
        mBinding.etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!TextUtils.isEmpty(editable.toString())){
                    mBinding.tvCancel.setVisibility(View.VISIBLE);
                }else {
                    mBinding.tvCancel.setVisibility(View.GONE);
                }
            }
        });
        mBinding.tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mBinding.etSearch.getText().clear();
                mBinding.tvCancel.setVisibility(View.GONE);
                keyWord = "";
            }
        });

    }

    @Override
    protected void start() {
        mAdapter = new BaseAdapter<CaseListAjBean>(mData, mContext) {
            @Override
            public void convert(BaseHolder holder, final CaseListAjBean item, boolean isHeader, boolean isBottom) {
                TextView tvName = holder.getView(R.id.tv_name);
                TextView tvAddress = holder.getView(R.id.tv_address);
                TextView tvDepartName = holder.getView(R.id.tv_dept_and_time);
                TextView tvMemBerName = holder.getView(R.id.tv_member);
                ImageView ivName = holder.getView(R.id.img_case);

                if (!TextUtils.isEmpty(item.getPic())) {
                    CaseListAjBean.FileBean fileBean = new Gson().fromJson(item.getPic(), CaseListAjBean.FileBean.class);
                    GlideUtil.load_nocatch(mContext, RealNetWorkUtil.getBaseUrl() + "/api-file/" + fileBean.getFilePath(), ivName);
                }

                tvName.setText(item.getCasename());
                tvAddress.setText(item.getDetailadress());
                tvMemBerName.setText(item.getCreateDeptName());
                tvDepartName.setText(item.getAccepttime());
            }
        };
        mAdapter.putNormalType(R.layout.item_case_manage);
        mAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseHolder holder, int dataPosition, boolean isHeader, boolean isBottom) {
                CaseListAjBean bean = mData.get(dataPosition);
                ARouter.getInstance().build("/case/casedetail")
                        .withString("cformId",bean.getFunctionId())
                        .withString("id", bean.getId())
                        .navigation();
            }
        });
        mBinding.rv.setLayoutManager(new LinearLayoutManager(mContext));
        mBinding.rv.setAdapter(mAdapter);
        mBinding.rv.addItemDecoration(new SpaceItemDecoration());

        mBinding.rl.autoRefresh();
        mBinding.rl.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                index ++;
                mPresenter.getCaseList(index,keyWord,ids);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                index =1;
                mPresenter.getCaseList(index,keyWord,ids);
            }
        });
    }

    @Override
    public void returnNoData() {
        mData.clear();
        mAdapter.notifyDataSetChanged();
        mBinding.rl.finishRefresh();
        mBinding.empty.setVisibility(View.VISIBLE);
    }

    @Override
    public void returnList(List<CaseListAjBean> list) {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.rl.finishRefresh();
        mBinding.rl.setLoadmoreFinished(false);
        mData.clear();
        mData.addAll(list);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void returnMoreList(List<CaseListAjBean> list) {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.rl.finishLoadmore();
        mBinding.rl.setLoadmoreFinished(false);
        mData.addAll(list);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void returnNoMoreData() {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.rl.finishLoadmore();
        mBinding.rl.setLoadmoreFinished(true);
    }

    @Override
    public void returnError() {
        mBinding.empty.setVisibility(View.GONE);
        if (index==1){
            mBinding.rl.finishRefresh();
        }else {
            mBinding.rl.finishLoadmore();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.release();
        Glide.get(this).clearMemory();
    }
}
